<?php 

	require_once(dirname(__FILE__)."/../core/safeParam.php");
	
	class cartWrapper{
		
		function getCatbyPage(){
			global $database;    
			global $config;
			global $lang;

			$id = $_GET['cid'];
			$content .= '<div id="menu">';
			
			$query = $database->query("SELECT * FROM menu_cat WHERE id = $id AND rid = ".RID."  order by pos");
			
			while ($list = @mysql_fetch_array($query)) {
				$mid = $list['id'];
				$name = $list['name'];
    			$price = $list['price'];
    			$description = $list['description'];
    			$price_types = unserialize($price);
				$price_titles = '';
				
				for ($i=0; $i<count($price_types); $i++){ 
					if ($price_types[$i] != ''){
						$price_titles .= '&nbsp;&nbsp;&nbsp;&nbsp;'.$price_types[$i].'';
					}
				}
				
				$prices1 = ''.$price_titles.'';
				$content .=  '
				<h2>
					<a name="menu'.$mid.'"></a>
					'.$name.'
				</h2>
				
				<p>'.$description.'</p>
				<p class="pricetype">'.$prices1.'</p>';
        		
        		$content .=  '
        		<script type="text/javascript">
        			function closed(){
        				alert(\''.$config['closed_message'].'\');  
            	}
            </script>';  
            
            $query_item = $database->query("SELECT * FROM menu_item WHERE menu = '$menuid' AND cat = '$mid'");
            
            while ($list_item = mysql_fetch_array($query_item)) {
            	$itemid = $list_item['id'];
            	$itemname = $list_item['name'];
            	$itemmenu = $list_item['menu'];
            	$itemdescription = $list_item['description'];
            	$itemprice = $list_item['price'];
            	$prices = unserialize($itemprice);
            	$itemfeature = $list_item['feature'];
            	$itemcat = $list_item['cat'];
            
            	$price_list = '';
            	
            	for( $h=0; $h < count($price_types); $h++){ 
            		if($price_types[$h] != '' && $prices[$h] != ''){
            			if($config['status'] == 1){
            				$price_list .= '
            				<a href="sites.php?act=viewprod&pid='.$itemid.'&price='.trim(price_format($prices[$h])).'" title="'.$itemname.'"   style="margin-left:10px" >
            					'.price_format($prices[$h]).'
            				</a>';
        					} else {
        						$price_list .= '
        						<a href="javascript:closed();" title="'.$itemname.'" style="margin-left:10px" >
        							'.price_format($prices[$h]).'
        						</a>';
        					}
        				}
        			}
        		
        		if($config['status'] == 1){
        			$content .=  '
        			<dl>
        				<dt>
        					<a href="sites.php?act=viewprod&pid='.$itemid.'" title="'.$itemname.'" >
        						'.  $itemname.'
        					</a>
        				</dt>
              		
              		<dd class="price">'.$price_list.'</dd>
              		<dd class="ingredients">'.$itemdescription.'</dd>
            	</dl>';
            } else {
            	$content .=  '
            	<dl>
            		<dt>
            			<a href="javascript:closed();" title="'.$itemname.'" >
            				'.  $itemname .'
            			</a>
            		</dt>
              		
              		<dd class="price">'.$price_list.'</dd>
            		<dd class="ingredients">'.$itemdescription.'</dd>
            	</dl>';
				}
			} 
		}
      
      return $content;
	}//getCatByPage
	
	function getMenus(){
		global $database;  
      
      $query = $database->query("SELECT * FROM menu order by id");
  		$list1 = "";
  		
  		while ($list = mysql_fetch_array($query)) {
  			$mid = $list['id'];
    		$name = $list['name'];
    		$list1 .='<a href="?mid='.$mid.'" > '.$name.'</a> ';
      }
  		
  		return '<div id="menus">'.$list1.'<div style="clear:both"></div></div>';
	}


	function getCartCategoriesNames() {
		global $database;
		$query1 = $database->select("SELECT * FROM menu_cat  WHERE rid = ".RID." order by pos");
  		
  		for ($i=0; $i<count( $query1); $i++){ 
  			$query1[$i]['open'] = openTime( $query1[$i]['hours'],'open');
			$query1[$i]['closed'] = openTime( $query1[$i]['hours'],'close');
		}
		
		return  $query1 ;
	}


	function getMenuList(){
		global $database;
		
		if (isset($_GET['mid'])){
			$mid = $_GET['mid'];
		} else {
			$mid = "";
		}
		
		if ($mid== ''){
			$mid = 1;
		} else {
			$mid = $mid;
 		}
      
      $query = $database->query("SELECT * FROM menu_cat  WHERE rid = ".RID."   ORDER by pos ");
      $content = "";
      
      while ($list = mysql_fetch_array($query)) {
      	$mid = $list['id'];
         $name = $list['name'];
         $price = $list['price'];
         $description = $list['description'];
         
         $content .=  '
         <div style="margin:10px">
         	<a name="menu'.$mid.'"></a>
         	<p style="font-size:1.1em; margin:0px; padding:0px">
         		'.$name.'
         	</p>
            <hr color="#a00000">
            <div>'.nl2br($description).'</div>
            <p style="font-weight:bold;margin:5px">Click sobre un producto para ordenar</p>
         </div>         
         <table  border="0" cellpadding="0" cellspacing="1" width="93%" align="center"> ';
         
       	$query_item = $database->query("SELECT * FROM menu_item WHERE cat = '$mid'");
         
         while ($list_item = mysql_fetch_array($query_item)) {
         	$itemid = $list_item['id'];
            $itemname = $list_item['name'];
            $itemmenu = $list_item['menu'];
            $itemdescription = $list_item['description'];
            $itemprice = $list_item['price'];
            $itemfeature = $list_item['feature'];
            $itemcat = $list_item['cat'];
            $itemtype = $list_item['type1'];
            $itemcode = $list_item['code'];
            
            $content .=  '
            <tr class="changmenu" valign="top">
            	<td width="5%">'.$itemcode.'</td>
            	
            	<td class="style33" width="5%">'.$itemtype.'</td>
            	
            	<td class="changmenubold" width="80%">
            		<a href="sites.php?act=viewprod&pid='.$itemid.'" title="'.$itemname.' - Price: $'.$itemprice.' "style="color:#000000;text-decoration:none">
            			'.$itemname.'
            		</a>
            		<br>  
            	</td> 
            	
            	<td width="10%">
                	<a href="sites.php?act=viewprod&pid='.$itemid.'" title="'.$itemname.' - Price: $'.$itemprice.' ">
                		<img src="images/basket_add.png" border="0"> '.$itemprice.'
                	</a>
               </td> 
            </tr>';
			}
                                          
         $content .= '</table>';
  		}
  		
  		return $content;
	}

	function getCartCategories() {
		global $database;
		
		$mid = $_GET['mid'];
		
		if ( $mid == ''){
			$mid = 1;
 		} else {
 			$mid = $mid;
 		}
 		
 		$query1 = $database->query("SELECT * FROM menu_cat  WHERE  rid = ".RID."  ");
 		
 		while ($list1 = mysql_fetch_array($query1)) {
 			$catid1 = $list1['id'];
        	$catname1 = $list1['name'];
        	$catmenu1 = $list1['menu'];
    		$description1 = $list1['description'];
    		$catname2 = str_replace(" ", "-", $catname1); 
  			$cats .= '<li><a href="sites.php?cid='.$catid1.'">'.$catname1.'</li>';  
		}
      
		return '<ul>
					<li><a href="pizza.php">Pizza</li>
					'.$cats.'</ul>';
	}

	function getCategoryItems($cid){
		global $database;
		global $pquery;
		
		$query3 = $database->query("SELECT * FROM menu_item WHERE cat = '$catid'");
		
		while ($list3 = mysql_fetch_array($query3)){
			$itemid = $list3['id'];
         $itemname = $list3['name'];
         $itemmenu = $list3['menu'];
         $itemdescription = $list3['description'];
         $itemprice = $list3['price'];
         $itemfeature = $list3['feature'];
         $itemcat = $list3['cat'];
        	$items .='<li>'.$pquery->link_to_remote("$itemname",array('url'=>'cart.php?action=add&name=$itemname&desc=$itemname&price= $itemprice','update'=>'#message','id'=>'id2')).'</li>';
    	}    
		
		return '    <ul>'.$items.'</ul>';
	}
	
	function getItemVars($pid, $type){
		global $database,$config;
		$item = $database->selectFirst("SELECT * FROM items WHERE id ='$pid'");
		
		if ($item) {
			return $item[$type];
  		}
	}
	
	/*PÁGINA CHECKOUT.PHP*/
	
	function cartFormReg($method, $payment){
		global $database, $config, $lang;
		
		$user = userArray($_SESSION['userid']);
		
		if(isset($_GET['d']) && $_GET['d'] == 1){
			$delivery_picked = 1;
  			$selected1 = '';
  			$selected2 = 'checked';
		} else {
  			$delivery_picked = 0;
  			$selected2 = '';
  			$selected1 = 'checked';
		}

		if (!isset($_GET['location'])){
  			$_GET['location'] = '';
		}
			
		$form = '
		<script type="text/javascript">
			<!--
			//Validar que el campo de formulario contenga sólo números
			
			function validacion(f) {
					if( (f.abono.value < '. ($_SESSION['sub'] + $_SESSION['tax']) .') || (isNaN(f.abono.value)) ){
						alert("Error:\nEl monto a abonar debe ser mayor o igual a '. ($_SESSION['sub'] + $_SESSION['tax']) .' .");
						f.abono.focus();
						return false;
					}
					
			}
			// -->
			</script>		
		';
		
		$form .= '<div id="checkout">';
		
		$form .='
		<form method="POST" action="sites.php?act=checkout&checkout=2&d='.$delivery_picked.'" onsubmit="return validacion(this)">
			<input type="hidden" name="id" value="'.$user['id'].'">
			<input type="hidden" value="1" name="send">
			
			<div style="padding:2px">
				<table cellpadding="3" cellspacing="3" cellpadding="6" border="1" width="100%" >';
    	
    	if(getOption('cb-only-delivery',RID) == 1){
    		$form .=  '
    				<input type="hidden" name="delivery" value="'.$lang['yes'].'">';
  		} 
		
		if($_SESSION['delivery'] == 1){
			$pickuptime = $config['deliverytime'];
		} else { 
			$pickuptime = $config['pickuptime'];  
		}
  		
  		$payment_Gatewayar = $database->select("SELECT * FROM gateway  where rid = ".RID." and enabled = 1 order by name");
  		$gateselect = '';

		for($i=0; $i<count($payment_Gatewayar); $i++){
			$gateselect .= '
			<option value="'.$payment_Gatewayar[$i]['name'].'">
				'.$payment_Gatewayar[$i]['display'].'
			</option>';
		}
		

  		$form .='
  		</table>  
  		</div>
  		
  		
		<table width="100%" cellpadding="2" cellspacing="5" border="0" >
  			<tr>
    			<td colspan="2" class="tdcartTitle">
    				<h1>Información del cliente</h1>
    			</td>
  			</tr>
			
			<tr>
				<td colspan="2" style="font-weight:normal; font-size:0.8em;">
    				'. $lang['order_final'] .'
				</td>
			</tr>  			
  			';
  		
  		/*CAMPOS REQUERIDOS*/
  		$checkout_fields = unserialize($config['checkout_fields']);
  		
  		if($checkout_fields[0] == 1){
  			$form .='
  			<tr>
    			<td align="right" width="250">
    				<b>'.$lang['fname'].':</b>
    			</td>
      					
      		<td style="font-weight:normal;"> 
      			'.$user['name'].'
      			<input type="hidden" name="name" value="'.$user['name'].'" class="required" />
      		</td>
  			</tr>';
		}
  		
  		if($checkout_fields[1] == 1){
  			$form .='
  			<tr>
  				<td align="right">
  					<b>'.$lang['lname'].':</b>
  				</td>
  							
      		<td style="font-weight:normal;">
      			'.$user['last_name'].'
      			<input type="hidden" name="last_name" value="'.$user['last_name'].'" class="required" />
      		</td>
    		</tr>';
    	}
    	
    	if($checkout_fields[2] == 1){
    		$form .='
    		<tr>
    			<td align="right">
    				<b>'.$lang['email'].':</b>
    			</td>
    						
      		<td style="font-weight:normal;">
      			'.$user['email'].'
      			<input type="hidden" name="email" value="'.$user['email'].'"  class="required" />
      		</td>
  			</tr>';
  		}
  		
  		if($checkout_fields[4] == 1){
  			$form .='
  			<tr>
    			<td align="right">
    				<b>'.$lang['phone'].':</b>
    			</td>
    						
      		<td style="font-weight:normal;">
      			'.$user['phone'].'
      			<input type="hidden" name="phone" value="'.$user['phone'].'"  class="required" />
      		</td>
  			</tr>';
   	}
  		
  		if($checkout_fields[3] == 1){
  			$form .='
  			<tr>
    			<td colspan="2">    		
	    				<h1>'.$lang['billing_address'].'</h1>
	    		</td>
	    	</tr>
	    	
	    	<tr>
	    		<td colspan="2" style="font-weight:normal; font-size:0.8em;">
	    			Dirección a la cuál se enviará el pedido.
    			</td>
  			</tr>';
  		
  			$form .='
  			<tr>
  				<td align="right">
  					<b>'.$lang['address'].':</b>
  				</td>
      		
      		<td style="font-weight:normal;">
      			'.$user['address'].'
      			<input type="hidden" name="address" value="'.$user['address'].'"   class="required"  />
      		</td>
  			</tr>';
  		}
  		  		    
      $form .='
      <tr>
    		<td colspan="2">
    			<div align="center" style="padding:10px">
    				<input type="checkbox" name="shipping_different" onclick="$(this).is(\':checked\') && $(\'#del_addy\').show() || $(\'#del_addy\').hide();"> '.$lang['different_delivery'].'
    			</div>
    		</td>
  		</tr>';
  					
  		$form .='
  		<tr align="left">
    		<td colspan="2" >
  				<div style="display:none" id="del_addy">
  					<table width="100%" cellpadding="3" cellspacing="3" border="0">';
  		
  		$form .=' 
  		<tr>
    		<td width="250" align="right">
    			<b>'.$lang['address'].':</b>
    		</td>
      	
      	<td>
      		<input type="text" name="delivery_address"  value="'.$user['address'].'" class="required" />
      	</td>
  		</tr>
  		</table>
  		</div>
  		</td>
  		</tr>';

		if($checkout_fields[8] == 0){  
    		$form .='   	   	
    		<tr>
    			<td colspan="2" class="tdcartTitle">
    				<h1>Forma de pago</h1>
    			</td>
    		</tr>
    
			<tr>
				<td colspan="2" align="center">
					<select name="gateway"> '.$gateselect.'</select>
				</td>
			</tr>
		
			<tr >
				<td align="right">
    				<b>Abono con:</b>
    			</td>	
    	
    			<td>
					<input type="text" name="abono" value="" class="required"/>
				</td>
  			</tr>';
		}
		
		$form .='
  	 	<tr>  	 	
  	 		<td colspan="2" class="tdcartTitle">
  	 			<h1>Comentarios</h1>
  	 		</td>
    	</tr>
    	
    	<tr>
    		<td colspan="2" style="font-weight:normal; font-size:0.8em;">
    		Por ejemplo: Vivo en un pasillo de puerta azul.
    		</td>
    	</tr>
    
    	<tr>    	
    		<td colspan="2" align="center">
					<textarea name="comments" rows="5" cols="30" class="required"></textarea>
			</td>
  		</tr>';
  	 
  	 	$form .='
  	 	<tr>
  	 		<td colspan="2" align="center">
  				<input type="submit" name="send_order" value="'.$lang['send_order'].' " style="padding:10px;" >
    		</td>
  		</tr>
		</table>
		</div>
		</form>
		<!-- END: form -->';
	
		return $form;
	}
}
?>
